package org.gridgain.internal.h2.util;

public class DistanceFunctions {

    public static Double euclideanDistance(Double[] u, Double[] v) {
        double sum = 0;
        for (int i = 0; i < u.length; i++) {
            double dp = u[i] - v[i];
            sum += dp * dp;
        }
        return Math.sqrt(sum);
    }

    public static Double euclideanDistance(double[] u, double[] v) {
        double sum = 0;
        for (int i = 0; i < u.length; i++) {
            double dp = u[i] - v[i];
            sum += dp * dp;
        }
        return Math.sqrt(sum);
    }

    public static Double cosineDistance(double[] u, double[] v) {
        double dot = 0.0f;
        double nru = 0.0f;
        double nrv = 0.0f;
        for (int i = 0; i < u.length; i++) {
            dot += u[i] * v[i];
            nru += u[i] * u[i];
            nrv += v[i] * v[i];
        }

        double similarity = dot / (Math.sqrt(nru) * Math.sqrt(nrv));
        return 1 - similarity;
    }

    public static Double cosineDistance(Double[] u, Double[] v) {
        double dot = 0.0f;
        double nru = 0.0f;
        double nrv = 0.0f;
        for (int i = 0; i < u.length; i++) {
            dot += u[i] * v[i];
            nru += u[i] * u[i];
            nrv += v[i] * v[i];
        }

        double similarity = dot / (Math.sqrt(nru) * Math.sqrt(nrv));
        return 1 - similarity;
    }
}
